<?php

namespace app\Login\controller;
use think\Controller;
use think\Request;
use think\Db;
use think\Session;


class Login extends Controller
{
    // 加载前台登录
     public function index()
    {   
        return view('Login@login/login');
    }
    
    // 验证登录
    public function loginto()
    {

        $res = input('post.');

        $tel = $res['phone'];
        $pwd = $res['password'];
        $sql = Db::table('chen_user')
                ->where('tel','eq',$tel)
                ->find();
        if ($sql['tel'] !== $tel or $sql['password'] !== md5($pwd)) {
            $info['data'] = false;
        }else{
      
          Session::set('tel',$tel);
          // dump($_SESSION);die;
          $info['data'] = true;
          return json($info);
        }
        return false;

    }

   

    public function register()
    {   
        if (Request::instance()->isAjax()) {
            $p= input('post.');
            $sin = Session::get('code');
            if ($p['smscode'] != $sin) {
                return ["message" => "验证码不对!"];
                
            }

            $data = [
              'name' => $p['name'],
              'tel' => $p['phone'],
              'password' => md5($p['password']),
            ];
            $re = Db::table('chen_user')
                ->where(array('tel'=>$data['tel']))
                ->find();
            if ($re > 0) {
                return ["message" => "该号码已被注册!"];
            }
            $result = Db::table('chen_user')->data($data)->insert();
                if ($result) {
                    return ['status'=>1,"message"=>"恭喜注册成功！"];

                }else{
                    return ['status'=>0,"message"=>"注册失败，请重试！"];
                }
            
        }else{
            $this->error('错误','/login');
        }
       
       
    }
    

    public function sendSMS()
    {
        $tel = input('tel');
        $curl = curl_init();
        $code = rand(1000,9999);
        Session::set('code',$code);
        curl_setopt_array($curl, array(
          CURLOPT_URL => "http://sms.yunpian.com/v2/sms/single_send.json",
          CURLOPT_RETURNTRANSFER => true,
          CURLOPT_ENCODING => "",
          CURLOPT_MAXREDIRS => 10,
          CURLOPT_TIMEOUT => 30,
          CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
          CURLOPT_CUSTOMREQUEST => "POST",
          CURLOPT_POSTFIELDS => "apikey=12e6a45915f312b8bf4d8bde3971aa18&mobile={$tel}&text=%E3%80%90Otokaze%E3%80%91%E6%82%A8%E7%9A%84%E9%AA%8C%E8%AF%81%E7%A0%81%E6%98%AF${code}&undefined=",
          CURLOPT_HTTPHEADER => array(
            "Content-Type: application/x-www-form-urlencoded",
            "Postman-Token: a1e891c0-e93d-4409-ac9c-455579a13848",
            "cache-control: no-cache"
          ),
        ));

        $response = curl_exec($curl);
        $err = curl_error($curl);
        // print_r($response);
    }
}